﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.DAO;
using SCRUM.Core.BusinessObjects;

/// <summary>
/// Usercontrol for selectiong a role
/// </summary>
public partial class RoleSelection : System.Web.UI.UserControl
{
    /// <summary>
    /// Gets the selected roles.
    /// </summary>
    /// <value>The selected roles.</value>
    public List<Role> SelectedRoles
    {
        get
        {
            List<Role> roles = new List<Role>();
            foreach (ListItem item in CheckBoxListRoles.Items)
            {
                if (item.Selected)
                {
                    roles.Add(DAORole.GetRoleByID(Convert.ToInt32(item.Value)));
                }
            }
            return roles;
        }
    }

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        DataBind();
    }

    /// <summary>
    /// Binds a data source to the invoked server control and all its child controls.
    /// </summary>
    public override void  DataBind()
    {
        base.DataBind();
        CheckBoxListRoles.DataSource = DAORole.GetAllRoles(false);
        CheckBoxListRoles.DataTextField = "Name";
        CheckBoxListRoles.DataValueField = "ID";
        CheckBoxListRoles.DataBind();
    }

    /// <summary>
    /// Clears the roles.
    /// </summary>
    public void Clear()
    {
        foreach (ListItem item in CheckBoxListRoles.Items)
        {
            item.Selected = false;
        }
    }
}
